//
//  PersonalData.m
//  TabBar
//
//  Created by 陈德锋 on 16/4/14.
//  Copyright © 2016年 陈德锋. All rights reserved.
//

#import "PersonalData.h"
@interface PersonalData()
@end
@implementation PersonalData

-(void) viewDidLoad
{
    self.tabBarController.tabBar.hidden = YES;
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0];
    [self setMember:_member];
    [self depa:_group];

    //设置导航栏背景透明，比你的方法简单多了，第一小前
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //   去除导航栏下的黑线
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];



}



- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsCompact];
}

- (void) loadView{
    [super loadView];
    _headView = [[UIView alloc]init];
    _headView.frame = CGRectMake(0, 0,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.width-80);
    _headView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
    [self.view addSubview:_headView];
    
    _userIcon = [[UIImageView alloc] init];
    _userIcon.frame = CGRectMake(0, 0, 50, 50);
    _userIcon.layer.shadowColor = [UIColor grayColor].CGColor;
//    [UIColor colorWithRed:205.0/255.0 green:205.0/255.0 blue:205.0/255.0 alpha:2.0].CGColor;
    _userIcon.layer.shadowOffset = CGSizeMake(6, 6);
    _userIcon.layer.shadowOpacity = 0.8;//阴影透明度，默认0
    _userIcon.image = [UIImage imageNamed:@""];
    CGFloat width = [[UIScreen mainScreen] bounds].size.width/2-(0.25*_headView.frame.size.height)/2;
    CGFloat heigth = ([[UIScreen mainScreen] bounds].size.width-80)/2;
    
    [_headView addSubview:_userIcon];
    [_userIcon setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_headView addConstraint:[NSLayoutConstraint constraintWithItem:_userIcon attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_headView attribute:NSLayoutAttributeLeft multiplier:1 constant:width]];
     [_headView addConstraint:[NSLayoutConstraint constraintWithItem:_userIcon attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_headView attribute:NSLayoutAttributeTop multiplier:1 constant:heigth]];
     [_headView addConstraint:[NSLayoutConstraint constraintWithItem:_userIcon attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_headView attribute:NSLayoutAttributeHeight multiplier:0.25 constant:0]];
    [_headView addConstraint:[NSLayoutConstraint constraintWithItem:_userIcon attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_headView attribute:NSLayoutAttributeHeight multiplier:0.25 constant:0]];
    _name = [[UILabel alloc] init];
    CGFloat Lwidth =[[UIScreen mainScreen] bounds].size.width/2;
    _name.frame = CGRectMake(Lwidth, 0, 50, 30);
   _name.textAlignment = NSTextAlignmentCenter;
    _name.text = @"";
    [_headView addSubview:_name];
    [_name setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_headView addConstraint:[NSLayoutConstraint constraintWithItem:_name attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_headView attribute:NSLayoutAttributeLeft multiplier:1 constant:Lwidth-25]];
    [_headView addConstraint:[NSLayoutConstraint constraintWithItem:_name attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_userIcon attribute:NSLayoutAttributeBottom multiplier:1 constant:10]];
    
    
    _phoneView = [[UIView alloc] init];
    _phoneView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.width-80,[[UIScreen mainScreen] bounds].size.width,45);
    _phoneView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_phoneView];
    _phone = [[UILabel alloc] init];
    _phone.frame = CGRectMake(10, 0, 40, 45);
    _phone.textColor = [UIColor colorWithRed:205.0/255.0 green:205.0/255.0 blue:205.0/255.0 alpha:1.5];
    _phone.text = @"手机";
    [_phoneView addSubview: _phone];
    _number = [[UILabel alloc] init];
    _number.frame = CGRectMake(95, 0, 120, 45);
    _number.text = @"18814129437";
    [_phoneView addSubview:_number];
    _phoneimage = [[UIImageView alloc] init];
    _phoneimage.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width-40, 7, 30, 30);
    _phoneimage.image = [UIImage imageNamed:@"phone"];
    [_phoneView addSubview:_phoneimage];
    _messageimage = [[UIImageView alloc]init];
    _messageimage.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width-75, 7, 30, 30);
    _messageimage.image = [UIImage imageNamed:@"message"];
    [_phoneView addSubview:_messageimage];
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(0, 0, _phoneView.frame.size.width, _phoneView.frame.size.height);
    [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_phoneView addSubview:_button];
    
    _depView = [[UIView alloc] init];
    _depView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.width-20,[[UIScreen mainScreen] bounds].size.width,45);
    _depView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: _depView];
    _depatment = [[UILabel alloc] init];
    _depatment.frame = CGRectMake(10, 0, 40, 45);
    _depatment.textColor = [UIColor colorWithRed:205.0/255.0 green:205.0/255.0 blue:205.0/255.0 alpha:1.5];
    _depatment.text = @"部门";
    [_depView addSubview: _depatment];
    _depa = [[UILabel alloc] init];
    _depa.frame = CGRectMake(95, 0, 120, 45);
    _depa.text = @"未设置";
    [_depView addSubview:_depa];
    

}

- (void)setMember:(Member *)member
{
    _member = member;
    if (_member) {
        _name.text = _member.name;
        
        NSString *imageName = [NSString stringWithFormat:@"%@.jpg",_member.icon];
       _userIcon.image = [UIImage imageNamed:imageName];
        
    }
}

-(void) depa:(Groups *) groups
{
    _group = groups;
    if (_group) {
        _depa.text = _group.name;
    }}


-(void)buttonClick:(UIButton *)sender
{
    UIAlertController *actionsheetController = [[UIAlertController alloc]init];
    actionsheetController.title = _number.text;
    UIAlertAction *canceAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
    }];
    UIAlertAction *phoneAction = [UIAlertAction actionWithTitle:@"打电话" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"tel://%@",_number.text]];
        [[UIApplication sharedApplication] openURL:url];
    }];
    UIAlertAction *messageAction = [UIAlertAction actionWithTitle:@"发短信" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        
      NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"sms://%@",_number.text]];    [[UIApplication sharedApplication] openURL:url];
    }];
    [actionsheetController addAction: canceAction];
    [actionsheetController addAction:phoneAction];
    [actionsheetController addAction:messageAction];
    [self presentViewController:actionsheetController animated:true completion:nil];
}




@end
